package com.woldier.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woldier.dto.DishDto;
import com.woldier.entity.Category;
import com.woldier.entity.Dish;
import com.woldier.entity.DishFlavor;
import com.woldier.entity.Employee;
import com.woldier.mapper.DishMapper;
import com.woldier.mapper.EmployeeMapper;
import com.woldier.service.CategoryService;
import com.woldier.service.DishFlavorService;
import com.woldier.service.DishService;
import com.woldier.service.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService{


    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;

    /**
     *
     * 保存菜品以及对应的风味
     * @param dishDto
     */
    @Override
    @Transactional /*加入注解 开启事务*/
    public void saveWithFlavor(DishDto dishDto) {
        List<DishFlavor> flavors = dishDto.getFlavors();


        /*保存菜品*/
        /*查询当前菜品是否存在*/


        /*不存在则保存 并且返回主键*/
        this.save(dishDto);

        /*这里的id一定要等save方法之后才能访问*/
        Long id = dishDto.getId();
        /*保存菜品的风味*/
        /*首先给flavor设置dish的id*/
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(id);
        }

        dishFlavorService.saveBatch(flavors);

    }

    /**
     * 分页返回 可更具name查询
     * 其中由两次查表 第一次查出菜品第二次查出对应的分类名
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    public Page<DishDto> getByPage(int page , int pageSize,String name){
        /*分页器*/
        Page<Dish> pageDish = new Page<Dish>(page,pageSize);

        /*Dto的分页器*/
        Page<DishDto> dishDtoPage = new Page<>();

        /*初始化条件构造器*/
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<Dish>();

        /*如果name 不为空 模糊查询蔡明*/
        lambdaQueryWrapper.like( (   !"".equals(name) && name !=null ), Dish::getName, name);

        /*分页查询 并且附带name字段的模糊查询*/
        this.page(pageDish,lambdaQueryWrapper);

        /*通过对象拷贝将pageDish拷贝到dishDtoPage 并且不拷贝records属性 */
        BeanUtils.copyProperties(pageDish,dishDtoPage,"records");

        /*查询到page后通过category_id 查询到菜品的分类名 放入dishDtoList中*/
        List<DishDto> dishDtoList = new ArrayList<>();
        for (Dish dish : pageDish.getRecords()) {
            DishDto dishDto = new DishDto();
            /*将dish中的属性拷贝到dishDto中*/
            BeanUtils.copyProperties(dish,dishDto);
            /*对每一个dish进行查询查询到page后通过category_id，获取名字*/
            Category category = categoryService.getById(dish.getCategoryId());
            /*从查询的数据中获取名称*/
            String categoryName = category.getName();
            /*设置给DishDto*/
            dishDto.setCategoryName(categoryName);
            dishDtoList.add(dishDto);
        }

        /*将得到的数组 设置给dishDtoList*/
        dishDtoPage.setRecords(dishDtoList);



        return dishDtoPage;
    }

    /**
     * 更新菜品信息和口味
     * @param dishDto
     */
    public void updateWithFlavor(DishDto dishDto){

        /*通过id进行更新彩屏分类以及其他信息*/
        this.updateById(dishDto);


        /*删除原有的*/
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper =new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(lambdaQueryWrapper);

        /*插入新增的*/
        /*由于dto中Flavors中的dishid为空，因此我们给他注入id*/
        Long id = dishDto.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(id);
        }
        dishFlavorService.saveBatch(flavors);

    }
}
